package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.defence.RtCode;
import com.migo.entity.DeviceRegisterEntity;
import com.migo.entity.UserDeviceRegisterEntity;
import com.migo.service.DeviceRegisterService;
import com.migo.service.UserDeviceRegisterService;
import com.migo.utils.R;
import com.migo.validator.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ApiUserDeviceRegisterController {


    //  设备清单
    @Autowired
    private DeviceRegisterService deviceRegisterService;
    //  用户设备注册清单
    @Autowired
    private UserDeviceRegisterService userDeviceRegisterService;

    /**
     * 注册设备清单查询
     */
    @IgnoreAuth
    @RequestMapping(value = "queryDevices", produces = "application/json;charset=utf-8;")
    public R queryDevices(@RequestParam Map<String, Object> params) {
        List<DeviceRegisterEntity> deviceList = deviceRegisterService.queryList(params);
        return R.ok().put("devices", deviceList);
    }


    /**
     * 设备PSN、设备型号、无人机登记号--保存
     */
    @IgnoreAuth
    @RequestMapping(value = "saveUserDeviceInfo", produces = "application/json;charset=utf-8;")
    public R saveUserDeviceInfo(@RequestParam Map<String, Object> params) {
        String userId = (String) params.get("userId");
        String deviceId = (String) params.get("deviceId");
        String deviceName = (String) params.get("deviceName");
        String devicePsn = (String) params.get("devicePsn");
        String realRegisterNumber = (String) params.get("realRegisterNumber");
        try {
            Assert.isBlank(userId, "userId is null");
            Assert.isBlank(deviceId, "deviceId is null");
            Assert.isBlank(deviceName, "deviceName is null");
            Assert.isBlank(devicePsn, "devicePsn is null");
            Assert.isBlank(realRegisterNumber, "realRegisterNumber is null");
            UserDeviceRegisterEntity userDeviceRegisterEntity = new UserDeviceRegisterEntity();
            userDeviceRegisterEntity.setDeviceId(Integer.parseInt(deviceId));
            userDeviceRegisterEntity.setDeviceName(deviceName);
            userDeviceRegisterEntity.setDevicePsn(devicePsn);
            userDeviceRegisterEntity.setRealRegisterNumber(realRegisterNumber);
            userDeviceRegisterEntity.setUserId(userId);
            userDeviceRegisterService.save(userDeviceRegisterEntity);
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @IgnoreAuth
    @RequestMapping(value = "delUserDeviceInfo", produces = "application/json;charset=utf-8;")
    public R delUserDeviceInfo(@RequestParam Map<String, Object> params) {
        String id = (String) params.get("id");
        try {
            Assert.isBlank(id, "id is null");
            userDeviceRegisterService.delete(Integer.parseInt(id));
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
        return R.ok();
    }

    /**
     * 查询各大厂商的
     */
    @IgnoreAuth
    @RequestMapping(value = "queryUserDeviceInfo", produces = "application/json;charset=utf-8;")
    public R queryUserDeviceInfo(@RequestParam Map<String, Object> params) {
        String userId = (String) params.get("userId");
        try {
            Assert.isBlank(userId, "userId is null");
            List<UserDeviceRegisterEntity> userDeviceRegisterEntities = userDeviceRegisterService.queryList(params);
            return R.ok().put("userDeviceList", userDeviceRegisterEntities);
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
    }


    /**
     * 设备PSN、设备型号、无人机登记号--更新
     */
    @IgnoreAuth
    @RequestMapping(value = "modifyUserDeviceInfo", produces = "application/json;charset=utf-8;")
    public R modifyUserDeviceInfo(@RequestParam Map<String, Object> params) {
        String id = (String) params.get("id");
        String deviceName = (String) params.get("deviceName");
        String devicePsn = (String) params.get("devicePsn");
        String realRegisterNumber = (String) params.get("realRegisterNumber");
        try {
            Assert.isBlank(id, "deviceId is null");
            Assert.isBlank(deviceName, "deviceName is null");
            Assert.isBlank(devicePsn, "devicePsn is null");
            Assert.isBlank(realRegisterNumber, "realRegisterNumber is null");
            UserDeviceRegisterEntity userDeviceRegisterEntity = userDeviceRegisterService.queryObject(Integer.parseInt(id));
            userDeviceRegisterEntity.setDeviceName(deviceName);
            userDeviceRegisterEntity.setDevicePsn(devicePsn);
            userDeviceRegisterEntity.setRealRegisterNumber(realRegisterNumber);
            userDeviceRegisterService.update(userDeviceRegisterEntity);
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
        return R.ok();
    }
}
